﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Alogient.Cameleon.Sdk.UI.WebControls
{
    [ToolboxData("<{0}:CMSitemap runat=server></{0}:CMSitemap>")]
    public class CMSitemap : TreeView
    {
        [Bindable(true)]
        [Category("Cameleon")]
        public int RootID { get; set; }

        [Bindable(true)]
        [Category("Cameleon")]
        public string CultureCode { get; set; }

        [Bindable(true)]
        [Category("Cameleon")]
        public int Depth { get; set; }

        [Bindable(true)]
        [Category("Cameleon")]
        public string TryField { get; set; }

        public CMSitemap()
        {
            this.Load += this.CMSitemap_Load;
        }

        public void CMSitemap_Load(object o, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                var root = CMServices.GetPage(this.RootID);
                if (root != null)
                {
                    var rootNode = new TreeNode { Text = root.PageTitle };

                    if (!string.IsNullOrEmpty(this.TryField))
                    {
                        if (root.Elements.Exists(this.TryField) && !string.IsNullOrEmpty(root.Elements[this.TryField].Value))
                        {
                            rootNode.Text = root.Elements[this.TryField].Value;
                        }
                    }

                    rootNode.NavigateUrl = root.NavigateUrl;

                    this.Nodes.AddAt(0, rootNode);
                    var subpages = root.Subpages;
                    this.AddNodes(subpages, rootNode);
                }
            }
        }

        private void AddNodes(Items.CMPageCollection aPageCollection, TreeNode parentNode)
        {
            if (aPageCollection != null && parentNode.Depth < this.Depth)
            {
                foreach (var page in aPageCollection)
                {
                    if (!page.PageNavEnd)
                    {
                        var currentNode = new TreeNode { Text = page.PageTitle };

                        if (!string.IsNullOrEmpty(this.TryField))
                        {
                            if (page.Elements.Exists(this.TryField) && !string.IsNullOrEmpty(page.Elements[this.TryField].Value))
                            {
                                currentNode.Text = page.Elements[this.TryField].Value;
                            }
                        }

                        currentNode.Value = page.NavID.ToString();
                        currentNode.ImageUrl = string.Empty;
                        currentNode.NavigateUrl = page.NavigateUrl;
                        parentNode.ChildNodes.Add(currentNode);

                        if (page.HasChildren)
                        {
                            Items.CMPageCollection subpages = page.Subpages;
                            this.AddNodes(subpages, currentNode);
                        }
                    }
                }
            }
        }

    }
}
